package com.vhall.component.service.version.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vhall.component.dao.version.VersionManageMapper;
import com.vhall.component.entity.version.VersionManage;
import com.vhall.component.entity.version.vo.RespVersionManagePage;
import com.vhall.component.entity.version.vo.VersionManageVO;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.service.version.VersionManageService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author jingwen.li
 */
@Service
public class VersionManageServiceImpl implements VersionManageService {

    @Resource
    private VersionManageMapper versionManageMapper;

    @Override
    public int add(VersionManageVO vo) {

        if (count(vo.getPlatform(), vo.getVersion()) > 0) {
            throw new BusinessException(60000, "数据已存在", "");
        }
        VersionManage versionManage = new VersionManage();
        BeanUtils.copyProperties(vo, versionManage);
        return versionManageMapper.insert(versionManage);
    }

    @Override
    public int update(VersionManageVO vo) {
        if (vo.getId() == null) {
            throw new BusinessException(BizErrorCode.BIZ_PARAM_NOT_NULL);
        }
        VersionManage versionManage = versionManageMapper.selectById(vo.getId());
        LambdaQueryWrapper<VersionManage> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(VersionManage::getPlatform, vo.getPlatform());
        queryWrapper.eq(VersionManage::getVersion, vo.getVersion());
        queryWrapper.last(" limit 1");
        VersionManage versionManageNew = versionManageMapper.selectOne(queryWrapper);
        if (versionManageNew != null) {
            if (versionManageNew.getPlatform().equals(versionManage.getPlatform())
                    && versionManageNew.getVersion().equals(versionManage.getVersion())) {
                VersionManage manage = new VersionManage();
                BeanUtils.copyProperties(vo, manage);
                return versionManageMapper.updateById(manage);
            } else {
                throw new BusinessException(60000, "数据已存在", "");
            }
        } else {
            VersionManage manage = new VersionManage();
            BeanUtils.copyProperties(vo, manage);
            return versionManageMapper.updateById(manage);
        }
    }

    @Override
    public int delete(Integer id) {
        if (id == null) {
            throw new BusinessException(BizErrorCode.BIZ_PARAM_NOT_NULL);
        }
        return versionManageMapper.deleteById(id);
    }

    @Override
    public VersionManageVO getInfo(Integer id) {
        VersionManage manage = versionManageMapper.selectById(id);
        if (manage == null) {
            throw new BusinessException(BizErrorCode.EMPTY_DATA);
        }
        VersionManageVO vo = new VersionManageVO();
        BeanUtils.copyProperties(manage, vo);
        return vo;
    }

    @Override
    public RespVersionManagePage pageList(String platform, int pageNum, int pageSize) {
        RespVersionManagePage resp = new RespVersionManagePage();
        Page<VersionManage> page1 = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<VersionManage> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StrUtil.isNotEmpty(platform), VersionManage::getPlatform, platform);
        queryWrapper.isNull(VersionManage::getDeleteTime);
        queryWrapper.orderByDesc(VersionManage::getId);
        Page<VersionManage> versionManagePage = versionManageMapper.selectPage(page1, queryWrapper);
        List<VersionManageVO> list = new ArrayList<>();
        if (versionManagePage.getRecords().size() > 0) {
            for (VersionManage manage : versionManagePage.getRecords()) {
                VersionManageVO vo = new VersionManageVO();
                BeanUtils.copyProperties(manage, vo);
                list.add(vo);
            }
        }
        resp.setList(list);
        resp.setCurrentPage(pageNum);
        resp.setLastPage((int) versionManagePage.getPages());
        resp.setPerPage(pageSize);
        resp.setTotal((int) versionManagePage.getTotal());
        resp.setFrom((pageNum - 1) * pageSize + 1);
        resp.setTo(Math.min(pageNum * pageSize, (int) versionManagePage.getTotal()));
        return resp;
    }

    @Override
    public VersionManageVO getInfo(String pf, String version) {
        LambdaQueryWrapper<VersionManage> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(VersionManage::getPlatform, pf);
        queryWrapper.eq(VersionManage::getVersion, version);
        queryWrapper.isNull(VersionManage::getDeleteTime);
        queryWrapper.last(" limit 1");
        VersionManage manage = versionManageMapper.selectOne(queryWrapper);
        VersionManageVO vo = new VersionManageVO();
        if (manage != null) {
            BeanUtils.copyProperties(manage, vo);
        } else {
            vo.setPlatform(pf);
            vo.setVersion(version);
            vo.setRoomModeCode(2);
            vo.setRoomModeName("SDK直播间");
        }
        return vo;
    }


    private Integer count(String pf, String version) {
        LambdaQueryWrapper<VersionManage> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(VersionManage::getPlatform, pf);
        queryWrapper.eq(VersionManage::getVersion, version);
        queryWrapper.isNull(VersionManage::getDeleteTime);
        return versionManageMapper.selectCount(queryWrapper);
    }
}
